package com.slicktechnologies.server;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.simplesoftwares.client.library.libservice.LoginService;
import com.slicktechnologies.shared.common.businessunitlayer.Company;
import com.slicktechnologies.shared.common.helperlayer.LoggedIn;
import com.slicktechnologies.shared.common.helperlayer.LoginProxy;
import com.slicktechnologies.shared.common.helperlayer.User;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7849431696181450915L;

	/*static{
		
		
		try{
			
			  ObjectifyService.register(Quotation.class);
			  ObjectifyService.register(Contract.class);
		  ObjectifyService.register(Config.class);
		  
		  ObjectifyService.register(Activity.class);
		  ObjectifyService.register(Company.class);
		  ObjectifyService.register(LoggedIn.class);
		  
		  ObjectifyService.register(Branch.class);
		  ObjectifyService.register(BranchRelation.class);
		  ObjectifyService.register(Employee.class);
		  ObjectifyService.register(EmployeeRelation.class);
		  ObjectifyService.register(User.class);
		  ObjectifyService.register(ItemProduct.class);
		  ObjectifyService.register(SuperProduct.class);
		  ObjectifyService.register(ServiceProduct.class);
		  ObjectifyService.register(Vendor.class);
		  ObjectifyService.register(Expense.class);
		  ObjectifyService.register(Lead.class);
		  ObjectifyService.register(Customer.class);
		  ObjectifyService.register(VendorRelation.class);
		  ObjectifyService.register(CustomerRelation.class);
		  ObjectifyService.register(Category.class);
		  ObjectifyService.register(Purchase.class);
		  ObjectifyService.register(LeaveCalendar.class);


		  ObjectifyService.register(Advance.class);
		  ObjectifyService.register(Country.class);
		  ObjectifyService.register(Grade.class);
		  ObjectifyService.register(LeaveType.class);


		  ObjectifyService.register(LeaveApplication.class);
		  ObjectifyService.register(Shift.class);
		  ObjectifyService.register(ShiftPattern.class);
		  ObjectifyService.register(Department.class);
		  
		  ObjectifyService.register(UserRole.class);
		  

		  ObjectifyService.register(com.slicktechnologies.shared.common.helperlayer.ConfigCategory.class);
		  ObjectifyService.register(com.slicktechnologies.shared.common.helperlayer.Type.class);

		
		  
		
		
		}
		catch(Exception e)
		{
			System.out.println("sumedh");
			e.printStackTrace();
			
		}
	    }
	*/
	
	
	
	
	@Override
	public User giveLogin(LoginProxy loginproxy) {
		User user;
		if(loginproxy.getCompanyName()==null)
			return null;
		//Company company=ofy().load().type(Company.class).filter("buisnessUnitName", loginproxy.getCompanyName()).first().now();
		Logger logger=Logger.getLogger("Komal");
		logger.log(Level.SEVERE, loginproxy.getCompanyName());
		Company company=ofy().load().type(Company.class).filter("accessUrl", loginproxy.getCompanyName()).first().now();
		if(company==null)
			return null;
		String companyStatus=company.getStatus();
		if(companyStatus.equals(Company.ACTIVE) || companyStatus.equals(Company.DEMO))
		{
			Long companyId=company.getCompanyId();
			System.out.println("company id------- "+companyId);
			logger.log(Level.SEVERE,"ID   "+companyId);
			user=ofy().load().type(User.class).filter("userName",loginproxy.getUserId()).filter("password", loginproxy.getPassword())
			.filter("companyId",companyId).first().now();
			if(user!=null)
			{
				LoggedIn loggedin=new LoggedIn();
				loggedin.setCompanyId(companyId);
				loggedin.setLoginTime(new Date());
				loggedin.setUserName(user.getUserName());
				ofy().save().entity(loggedin).now();
				logger.log(Level.SEVERE,"User ID   "+user.getId());
				
				return user;
			}
			else 
				return null;	
		}
		else
			return null;
	}
	
}
